﻿var cube1;
var cube2;
var cube3;
var host = 'shaizi.cnodejs.net';
var port = 8080;
var socketurl = 'ws://' + host + ':' + port + '/';
var w;
var LogedIn = false
$(document).ready(function() {
    cube1 = new cube("#cube1");
    cube2 = new cube("#cube2");
    cube3 = new cube("#cube3");
    cube1.rotate(45, 45, 0);
    cube2.rotate(135, 45, 0);
    cube3.rotate(45, 135, 0);
    createSocket(socketurl);
});
function createSocket(url) {
    w = new WebSocket(url);
    w.onmessage = function(data) {
        //alert(data.data);
        if (LogedIn) {
            var obj = $.parseJSON((data.data));
            //alert(obj[0].type);
            switch (parseInt(obj[0].type)) {
                case 1:
                    finish(obj[0].draw.split(","));
                    break;
                case 3:
                    appendUsers(obj);
                    break;
                default:
                    alert(obj);
            }
        }
    }
    w.onopen = function() {
        w.opened = true;
        cube1.roll();
        cube2.roll();
        cube3.roll();
        buttonsBind(true);
    }
    w.onclose = function() {
        buttonsBind(false);
        cube1.to(1);
        cube2.to(1);
        cube3.to(1);
        w.opened = false;
    }
    w.onerror = function() {
        buttonsBind(false);
        cube1.to(2);
        cube2.to(2);
        cube3.to(2);
        w.opened = false;
    }
}
function appendUsers(list) {
    var result = "";
    var num = list.length;
    //alert(num);
    var bet;
    for (var i = 0; i < num; i++) {
        //alert(list[i].bet);
        switch (parseInt(list[i].bet)) {
            case 0:
                bet = "<a class='button green'>小</a>";
                break;
            case 1:
                bet = "<a class='button red'>大</a>";
                break;
            case 2:
                bet = "<a class='button orange'>豹</a>";
                break;
            default:
                alert("bet:" + list[i].bet);
        }
        result = result + "<div class='user' id='" + list[i].name + "'><li class='userBet'>" + bet + "</li><li class='userName'>" + list[i].name + "</li><li class='userMoney'>￥" + list[i].amt + "</li></div>";
    }
    //alert(result);
    $("#right").html(result);
}

function finish(array) {
    var result;
    cube1.to(array[0]);
    cube2.to(array[1]);
    cube3.to(array[2]);
    if (array[0] == array[1] && array[0] == array[2]) {
        result = "baozi";
    } else {
        var total = parseInt(array[0]) + parseInt(array[1]) + parseInt(array[2]);
        if (total < 11) {
            result = "small";
        } else {
            result = "big";
        }
    }
    var amount = $("#money #amount");
    var remain = $("#name #moneyRemain #number");
    var bonus;
    if ($("#bet .selected").attr("id") == result) {
        if (result == "baozi") {
            bonus = parseInt(amount.html()) * 36;
        } else {
            bonus = parseInt(amount.html()) * 2;
        }
        remain.html(parseInt(remain.html()) + bonus);
    }
    amount.html("0");
    $("#right").html("");
    LogedIn = false;
    buttonsBind(true);
}

function buttonsBind(bool) {
    if (bool) {
        //buttons
        $("#bet .button").removeClass("selected").removeClass("unselected");
        $(".button").unbind("click");
        $(".button").bind("click", function() {
            return false;
        });
        $("#bet .button").bind("click", function() {
            $(this).parent().find(".button").addClass("unselected").removeClass("selected");
            $(this).removeClass("unselected").addClass("selected");
            return false;
        });
        $("#money #add").bind("click", function() {
            var allow = true;
            var remain = $("#name #moneyRemain #number");
            var amount = $(this).parent().find("#amount");
            if (parseInt(remain.html()) - parseInt(amount.html()) < 10) {
                allow = false;
                Shake(remain.parent());
            }
            if (allow) {
                amount.html(parseInt(amount.html()) + 10);
            }
            return false;
        });
        $("#money #minus").bind("click", function() {
            var allow = true;
            var amount = $(this).parent().find("#amount");
            if (parseInt(amount.html()) <= 10) {
                allow = false;
                Shake(amount);
            }
            if (allow) {
                amount.html(parseInt(amount.html()) - 10);
            }
            return false;
        });
        $("#submitArea #submit").bind("click", function() {
            var allow = true;
            var nameInput = $("#nameInput");
            var nameDisplay = $("#nameDisplay");
            if (nameInput.val() == "") {
                allow = false;
                Shake(nameInput);
            }
            var bets = $("#bet .selected");
            if (bets.length != 1) {
                allow = false;
                Shake($("#bet .button"));
            }
            var amount = $("#money #amount");
            var remain = $("#name #moneyRemain #number");
            if (10 > parseInt(amount.html()) || parseInt(amount.html()) > parseInt(remain.html())) {
                Shake(amount);
                allow = false;
            }
            if (allow) {
                nameInput.hide(0);
                nameDisplay.html(nameInput.val()).css("display", "inline-block");
                remain.html(parseInt(remain.html()) - parseInt(amount.html()));
                var name = nameInput.val();
                var amt = amount.html();
                var bet;
                switch (bets.attr("id")) {
                    case "small":
                        bet = 0;
                        break;
                    case "big":
                        bet = 1;
                        break;
                    case "baozi":
                        bet = 2;
                        break;
                    default:
                        alert("Bet错误");
                }
                var message = '{"type":3,"name":"' + name + '","bet":' + bet + ',"amt":' + amt + '}';
                if (w.opened) {
                    w.send(message);
                    buttonsBind(false);
                    cube1.roll();
                    cube2.roll();
                    cube3.roll();
                    LogedIn = true;
                }
            }
            return false;
        });
        //buttons end
    } else {
        $(".button").unbind("click");
        $(".button").bind("click", function() {
            return false;
        });
    }
}

function cube(thing) {
    var o = this;
    this.cube = $(thing);
    this.x = 0;
    this.y = 0;
    this.z = 0;
    this.rotate = function(x, y, z) {
        o.x = o.x + x;
        o.y = o.y + y;
        o.z = o.z + z;
        //alert("x="+this.x+" y="+this.y);
        o.cube.css("-webkit-transform", "rotateX(" + o.x + "deg) rotateY(" + o.y + "deg) rotateZ(" + o.z + "deg)");
    }
    this.roll = function() {
        if (o.timer == undefined) {
            o.timer = setInterval(function() {
                o.rotate(120, 120, 0);
            }, 300);
        }
    }
    this.to = function(result) {
        o.x = 0;
        o.y = 0;
        o.z = 0;
        result = parseInt(result);
        clearInterval(o.timer);
        o.timer = undefined;
        switch (result) {
            case 1:
                o.rotate(-90, 0, 0);
                break;
            case 2:
                o.rotate(0, 0, 0);
                break;
            case 3:
                o.rotate(0, -90, 0);
                break;
            case 4:
                o.rotate(0, 90, 0);
                break;
            case 5:
                o.rotate(180, 0, 180);
                break;
            case 6:
                o.rotate(-90, 0, 180);
                break;
            default:
                o.roll();
        }
    }
}

function Shake(objs) {
    $(objs).each(function(index, obj) {
        $(obj).animate({marginLeft:"-=2px"}, 20, function() {
            $(obj).animate({marginLeft:"+=5px"}, 20, function() {
                $(obj).animate({marginLeft:"-=5px"}, 20, function() {
                    $(obj).animate({marginLeft:"+=5px"}, 20, function() {
                        $(obj).animate({marginLeft:"-=5px"}, 20, function() {
                            $(obj).animate({marginLeft:"+=2px"}, 20);
                        });
                    });
                });
            });
        });
    });
}